package com.swang.pms.model.entity;
 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName: DhtmlxTreeMapXml
 * @Description:
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class DhtmlxTreeMapXml{
	public static String generateTreeMapXml(List list){
		HashMap<String, HashMap> tree = new HashMap<String, HashMap>();
		HashMap<String, Object>  value = new HashMap<String, Object>();
		value.put("id", "0");
		value.put("text", "root");
		value.put("key_child", new ArrayList());
		if (list!=null && !list.isEmpty() ){
			for (int i = 0; i < list.size(); i++) {
				HashMap itree = (HashMap)list.get(i);
				String id =  itree.get("id").toString();
				String parentid = itree.get("parentid").toString();
				HashMap pnode = tree.get(parentid);
				if(itree.get("level")!=null&&"3".equals(itree.get("level").toString())){
					if(pnode.get("buttons") == null){
						pnode.put("buttons", itree.get("id"));
					}else{
						pnode.put("buttons", pnode.get("buttons")+","+itree.get("id"));
					}
				}else if (pnode != null){
					if (pnode.get("key_child") == null){
						pnode.put("key_child", new ArrayList());
					}
					((ArrayList)pnode.get("key_child")).add(itree);
				} else {
					((ArrayList)value.get("key_child")).add(itree);
				}
				tree.put(id, itree);
			}
		}
		StringBuffer buf = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		generateTreeXml(value,buf);
		return buf.toString();
	}
	
	private static void generateTreeXml(HashMap hash,StringBuffer buf){
		if (hash.get("id") != null && hash.get("id").equals("0")){
			buf.append("<tree id=\""+hash.get("id")+"\" text=\""+hash.get("text")+"\" >");
		} else{
			buf.append("<item id=\"");
			buf.append(hash.get("id"));
			buf.append("\" text=\"");
			buf.append(hash.get("text"));
			buf.append("\" im0=\"");
			buf.append(hash.get("img"));
			buf.append("\" im1=\"");
			buf.append(hash.get("img"));
			buf.append("\" im2=\"");
			buf.append(hash.get("img"));
			buf.append("\"><userdata name=\"type\">1</userdata>");
		}
		ArrayList list =  null;
		for (Iterator iterator = hash.keySet().iterator(); iterator.hasNext();) {
			Object type = iterator.next();
			if (type.toString().equals("key_child")) { 
				list = (ArrayList)hash.get(type);
			} else {
				Object temp = hash.get(type);
				if (temp != null){
					buf.append("<userdata name=\"");
					buf.append(type.toString());
					buf.append("\">");
					buf.append(hash.get(type).toString() );
					buf.append("</userdata>");
				}
			}
		}
		if (list != null){
			for (Iterator iterator2 = list.iterator(); iterator2.hasNext();) {
				HashMap object = (HashMap) iterator2.next();
				generateTreeXml(object,buf);
			}
		}
        generateTreeXmlEnd( hash, buf);
	}

    private static void generateTreeXmlEnd(HashMap hash,StringBuffer buf){
        if (hash.get("id") != null && hash.get("id").equals("0")){
            buf.append("</tree>");
        } else {
            buf.append("</item>");
        }
    }
}
